/**
 * 邮箱奖励领取模块
 */
let isUtilsLoaded = false
var utils = { autoJSUtil: null, gestureUtil: null }

function main() {
    // 判断是否在当前阶段
    console.warn('邮件领取模块执行...')
    const levelingModuleStorage = storages.create('levelingModuleStorage')
    if (!levelingModuleStorage.get('complete')) {
        console.error('优先等级提升')
        return
    }
    const mailPullModuleStorage = storages.create('mailPullModuleStorage')
    if (mailPullModuleStorage.get('complete')) {
        console.error('今日已完成邮件领取')
        return
    }
    if (!isUtilsLoaded) {
        loadUtils()
        isUtilsLoaded = true
    }
    console.error(utils)
    // 业务逻辑
    const pos = utils.imageUtil.findImageInCapture('mail/mail.png', 0.6)
    if (pos) {
        click(pos.x, pos.y)
        sleep(2500)
        const p2 = utils.imageUtil.findImageInCapture('mail/onepull.png', 0.6)
        if (p2) {
            click(p2.x, p2.y)
            sleep(1000)
            click(p2.x, p2.y)
            utils.imageUtil.findImageClick('mail/close.png', 0.8)
        } else {
            utils.imageUtil.findImageClick('mail/close.png', 0.8)
        }
        mailPullModuleStorage.put('complete', true)
    }
}

function loadUtils() {
    const configStorage = storages.create('config')
    utils.autoJSUtil = eval(configStorage.get('autoJSUtil'))
    utils.gestureUtil = eval(configStorage.get('gestureUtil'))
    utils.imageUtil = eval(configStorage.get('imageUtil'))
}

main()
